package com.geek.hotupdate.config;

import com.geek.hotupdate.beans.ApiMetrics;
import com.geek.hotupdate.beans.DbMetrics;
import com.geek.hotupdate.beans.ScheduledUpdater;
import com.geek.hotupdate.beans.SimpleHttpServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

@Component
public class ApplicationInitialize implements ApplicationRunner {
    @Autowired
    private KafkaConfig kafkaConfig;
    @Autowired
    private MysqlConfig mysqlConfig;
    @Autowired
    private RedisConfig redisConfig;
    @Autowired
    private ApiMetrics apiMetrics;
    @Autowired
    private DbMetrics dbMetrics;

    @Override
    public void run(ApplicationArguments args) {
        ScheduledUpdater redisConfigUpdater = new ScheduledUpdater(300, 300, redisConfig::update);
        redisConfigUpdater.run();

        ScheduledUpdater kafkaConfigUpdater = new ScheduledUpdater(60, 60, kafkaConfig::update);
        kafkaConfigUpdater.run();

        SimpleHttpServer simpleHttpServer = new SimpleHttpServer("127.0 .0 .1", 2389);
        simpleHttpServer.addViewer("/config", redisConfig)
                .addViewer("/config", mysqlConfig)
                .addViewer("/metrics", apiMetrics)
                .addViewer("/metrics", dbMetrics)
                .run();
    }
}
